# -*- python -*-
# ex: set syntax=python:

# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from buildbot.schedulers.basic import SingleBranchScheduler

from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory
from master.v8.tools import verify_subdir_slaves

import config
import master_site_config

ActiveMaster = master_site_config.V8Ports
MAIL_NOTIFIER = ActiveMaster.is_production_host

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}
c['status'] = []


####### DATABASE

config.DatabaseSetup(c)


####### CHANGESOURCES

comparator = gitiles_poller.GitilesRevisionComparator()
c['change_source'] = [
  gitiles_poller.GitilesPoller(
      'https://chromium.googlesource.com/v8/v8',
      branches=['master'],
      pollInterval=20,
      comparator=comparator,
  )
]


####### FACTORIES

F = annotator_factory.AnnotatorFactory(ActiveMaster).BaseFactory


####### BUILDER SPECS

CATEGORY_ARM = '1Arm|arm'
CATEGORY_ARM64 = '1Arm64|arm64'
CATEGORY_MIPS = '2Mips|mips'
CATEGORY_PPC = '3PPC|ppc'
CATEGORY_S390 = '4S390|s390'
CATEGORY_X87 = '5X87|x87'

scheduled_builders = [
  {
    'name': 'V8 Arm - builder',
    'factory': F('v8'),
    'category': CATEGORY_ARM,
    'mergeRequests': False,
  },
  {
    'name': 'V8 Arm - debug builder',
    'factory': F('v8'),
    'category': CATEGORY_ARM,
  },
  {
    'name': 'V8 Android Arm - builder',
    'factory': F('v8'),
    'category': CATEGORY_ARM,
    'mergeRequests': False,
  },
  {
    'name': 'V8 Linux - arm - sim',
    'factory': F('v8'),
    'category': CATEGORY_ARM,
  },
  {
    'name': 'V8 Linux - arm - sim - debug',
    'factory': F('v8'),
    'category': CATEGORY_ARM,
  },
  {
    'name': 'V8 Android Arm64 - builder',
    'factory': F('v8'),
    'category': CATEGORY_ARM64,
    'mergeRequests': False,
  },
  {
    'name': 'V8 Linux - arm64 - sim',
    'factory': F('v8'),
    'category': CATEGORY_ARM64,
  },
  {
    'name': 'V8 Linux - arm64 - sim - debug',
    'factory': F('v8'),
    'category': CATEGORY_ARM64,
  },
  {
    'name': 'V8 Linux - arm64 - sim - nosnap - debug',
    'factory': F('v8'),
    'category': CATEGORY_ARM64,
  },
  {
    'name': 'V8 Linux - arm64 - sim - gc stress',
    'factory': F('v8'),
    'category': CATEGORY_ARM64,
  },
  {
    'name': 'V8 Mips - builder',
    'factory': F('v8'),
    'category': CATEGORY_MIPS,
  },
  {
    'name': 'V8 Linux - mipsel - sim - builder',
    'factory': F('v8'),
    'category': CATEGORY_MIPS,
  },
  {
    'name': 'V8 Linux - mips64el - sim - builder',
    'factory': F('v8'),
    'category': CATEGORY_MIPS,
  },
  {
    'name': 'V8 Linux - ppc - sim',
    'factory': F('v8'),
    'category': CATEGORY_PPC,
  },
  {
    'name': 'V8 Linux - ppc64 - sim',
    'factory': F('v8'),
    'category': CATEGORY_PPC,
  },
  {
    'name': 'V8 Linux - s390 - sim',
    'factory': F('v8'),
    'category': CATEGORY_S390,
  },
  {
    'name': 'V8 Linux - s390x - sim',
    'factory': F('v8'),
    'category': CATEGORY_S390,
  },
  {
    'name': 'V8 Linux - x87 - nosnap - debug builder',
    'factory': F('v8'),
    'category': CATEGORY_X87,
  },
]

triggered_builders = [
  {
    'name': 'V8 Arm',
    'factory': F('v8'),
    'category': CATEGORY_ARM,
    'auto_reboot' : False,
    'slavebuilddir': 'swarming',
  },
  {
    'name': 'V8 Arm - debug',
    'factory': F('v8'),
    'category': CATEGORY_ARM,
    'auto_reboot' : False,
    'slavebuilddir': 'swarming',
  },
  {
    'name': 'V8 Arm GC Stress',
    'factory': F('v8'),
    'category': CATEGORY_ARM,
    'auto_reboot' : False,
    'slavebuilddir': 'swarming',
  },
  {
    'name': 'V8 Mips - big endian - nosnap - 1',
    'factory': F('v8'),
    'category': CATEGORY_MIPS,
    'auto_reboot' : True,
  },
  {
    'name': 'V8 Mips - big endian - nosnap - 2',
    'factory': F('v8'),
    'category': CATEGORY_MIPS,
    'auto_reboot' : True,
  },
  {
    'name': 'V8 Linux - mipsel - sim',
    'factory': F('v8'),
    'category': CATEGORY_MIPS,
    'auto_reboot' : False,
  },
  {
    'name': 'V8 Linux - x87 - nosnap - debug',
    'factory': F('v8'),
    'category': CATEGORY_X87,
    'auto_reboot' : False,
  },
]


####### SCHEDULERS

c['schedulers'] = [
  SingleBranchScheduler(
      name='v8_ports',
      branch='master',
      treeStableTimer=10,
      builderNames=[builder['name'] for builder in scheduled_builders],
  ),
]


####### BUILDERS

c['builders'] = (
    scheduled_builders +
    triggered_builders
)

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'V8Ports')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)

# Make sure subdir slaves don't auto-reboot.
verify_subdir_slaves(c)

####### STATUS TARGETS

# Buildbot master url:
# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html='../master.chromium/public_html',
    tagComparator=comparator,
    templates=['../master.client.v8/templates',
               '../master.chromium/templates'])

if MAIL_NOTIFIER:
  import mail_notifier_cfg
  mail_notifier_cfg.Update(config, ActiveMaster, c)

# Adjust the buildCaches to be 3x the number of slaves per builder.
c['autoBuildCacheRatio'] = 3


####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
